package com.wsb.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wsb.entry.SmbmsRole;
import com.wsb.entry.SmbmsUser;
import com.wsb.service.SmbmsUserService;
import com.wsb.entry.SmbmsRole;
import com.wsb.entry.SmbmsUser;
import com.wsb.service.SmbmsUserService;
import com.wsb.service.impl.SmbmsUserServiceimpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
public class SmbmsUserController {
    @Autowired
    SmbmsUserService smbmsUserService;
    @RequestMapping("/jsp/useraddview")
    public String useraddview(){
        return "jsp/useradd";
    }
    @RequestMapping("/jsp/queryUser.do")
    public String userQuery(HttpServletRequest req, HttpServletResponse resp){
        String pageIndex=req.getParameter("pageIndex");
        String queryname=req.getParameter("queryname");
        String queryUserRole=req.getParameter("queryUserRole");

        Page page=new Page();
        if(pageIndex==null ||pageIndex==""){
            page.setCurrent(1);
        }else{
            page.setCurrent(Long.valueOf(pageIndex));
        }
        page.setSize(5);
        SmbmsUser smbmsUser=new SmbmsUser();
        if(queryname!=null&&queryname!=""){
            smbmsUser.setUserName(queryname);
        }
        if(queryUserRole!=null&&queryUserRole!=""){
            smbmsUser.setUserRole(Integer.valueOf(queryUserRole));
        }
        IPage<SmbmsUser> smbmsUserIPage= smbmsUserService.getSmbmsUserByPage(page,smbmsUser);
        //准备数据 从数据库当中查找数据，并放到session 当中去
        //servlet ==>service ==> dao
        List<SmbmsRole>  smbmsRoleList = smbmsUserService.getSmbmsRoleAll();
        HttpSession session = req.getSession();
        session.setAttribute("userList",smbmsUserIPage.getRecords());
        session.setAttribute("smbmsRoleList",smbmsRoleList);
        //总页数
        session.setAttribute("totalPageCount",smbmsUserIPage.getPages());

        //总条数
        session.setAttribute("totalCount",smbmsUserIPage.getTotal());
        //当前页
        session.setAttribute("currentPageNo",smbmsUserIPage.getCurrent());
        return "/jsp/userlist";

    }

    @RequestMapping("/jsp/userAdd.do")
    protected String userAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("添加用户信息");
        req.setCharacterEncoding("UTF-8");//客户端网页我们控制为UTF-8
        String userCode =  req.getParameter("userCode");
        String userName =  req.getParameter("userName");
        String userPassword =  req.getParameter("userPassword");
        String gender =  req.getParameter("gender");
        String birthday =  req.getParameter("birthday");
        String phone =  req.getParameter("phone");
        String userRole =  req.getParameter("userRole");
        String address  = req.getParameter("address");
//        SmbmsUserService smbmsUserService = new SmbmsUserServiceImpl();
        SmbmsUser smbmsUser= new SmbmsUser();
        smbmsUser.setUserCode(userCode);
        smbmsUser.setUserName(userName);
        smbmsUser.setUserPassword(userPassword);
        smbmsUser.setGender(Integer.valueOf(gender));
        SimpleDateFormat  simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date birthdayDate =  simpleDateFormat.parse(birthday);
            smbmsUser.setBirthday(birthdayDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        smbmsUser.setPhone(phone);
        smbmsUser.setUserRole(Integer.valueOf(userRole));
        smbmsUser.setAddress(address);
        smbmsUserService.addSmbmsUser(smbmsUser);
        return "forward:/jsp/queryUser.do";
    }

    @RequestMapping("/jsp/userDel.do")
    @ResponseBody
    protected  String userDel(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uid = req.getParameter("uid");
      //  SmbmsUserService smbmsUserService = new SmbmsUserServiceImpl();
        smbmsUserService.delSmbmsUser(Long.valueOf(uid));
//        resp.setContentType("text/json; charset=utf-8");
//        PrintWriter out = resp.getWriter();
//        out.println("{\"delResult\":\"true\"}");
//        HashMap<String,String> hashMap = new HashMap<>();
//        hashMap.put("delResult","true");
        return  "{\"delResult\":\"true\"}";
    }


    @RequestMapping("/jsp/userView.do")
    protected String userView(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userId = req.getParameter("uid");
        System.out.println(userId);
       // SmbmsUserService smbmsUserService = new SmbmsUserServiceImpl();
        SmbmsUser smbmsUser =  smbmsUserService.getSmbmsUserById(Long.valueOf(userId));
        List<SmbmsRole> smbmsRoleList = smbmsUserService.getSmbmsRoleAll();
        HttpSession session = req.getSession();
        session.setAttribute("user",smbmsUser);
        session.setAttribute("smbmsRoleList",smbmsRoleList);
        return "/jsp/userview";
    }

    @RequestMapping("/jsp/userUpdateView.do")
    protected String userUpdateView(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userId = req.getParameter("uid");
        System.out.println(userId);
//        SmbmsUserService smbmsUserService = new SmbmsUserServiceImpl();
        SmbmsUser smbmsUser =  smbmsUserService.getSmbmsUserById(Long.valueOf(userId));
        HttpSession session = req.getSession();
        session.setAttribute("user",smbmsUser);
        return "/jsp/usermodify";
    }
    @RequestMapping("/jsp/userUpdate.do")
    protected String userUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("修改用户信息");
        req.setCharacterEncoding("UTF-8");//客户端网页我们控制为UTF-8
        String userCode =  req.getParameter("userCode");
        String id =  req.getParameter("uid");
        String userName =  req.getParameter("userName");
        String userPassword =  req.getParameter("userPassword");
        String gender =  req.getParameter("gender");
        String birthday =  req.getParameter("birthday");
        String phone =  req.getParameter("phone");
        String userRole =  req.getParameter("userRole");
        String address  = req.getParameter("address");
       // SmbmsUserService smbmsUserService = new SmbmsUserServiceImpl();
        SmbmsUser smbmsUser= new SmbmsUser();
        smbmsUser.setId(Long.valueOf(id));
        smbmsUser.setUserCode(userCode);
        smbmsUser.setUserName(userName);
        smbmsUser.setUserPassword(userPassword);
        smbmsUser.setGender(Integer.valueOf(gender));
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date birthdayDate =  simpleDateFormat.parse(birthday);
            smbmsUser.setBirthday(birthdayDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        smbmsUser.setPhone(phone);
        smbmsUser.setUserRole(Integer.valueOf(userRole));
        smbmsUser.setAddress(address);
        smbmsUserService.updSmbmsUser(smbmsUser);
        return "forward:/jsp/queryUser.do";
    }
}
